﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace GoodMan.Infrastructure.Entities.Repository
{
    [Table("T_Order")]
    public class T_Order : IEntity<T_Order>
    {
        [Key]
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
        public int Id{get;set;}
        public DateTime OrderDate { get; set; }
        [Column(TypeName="decimal")]
        public decimal TotalPrice { get; set; }
        public int AreaCode { get; set; }
        public int Status { get; set; }
        [Column(TypeName="varchar")]
        [StringLength(128)]
        public string UserName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(32)]
        public string OrderCode { get; set; }
        /// <summary>
        /// 配送信息：张三（手机：13100000000） 楼栋 1310
        /// </summary>
        [Column(TypeName = "varchar")]
        [StringLength(1024)]
        public string AssemblingInfo { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(64)]
        public string Phone { get; set; }
        public int DormitoryID { get; set; }
        /// <summary>
        /// 所购买的产品简单描述 例：（友友泡凤爪(125g)*2、统一老坛酸菜牛肉面（桶）*1、）
        /// 格式为产品名称[(规格)]*购买数量
        /// </summary>
        [Column(TypeName = "nvarchar")]
        [StringLength(1024)]
        public string ProductDescript { get; set; }

        public int Payway { get; set; }

        [Column(TypeName = "varchar")]
        [StringLength(512)]
        public string Remark { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string HandleUser { get; set; }
        /// <summary>
        /// 商户ID
        /// </summary>
        public int BusinessID { get; set; }

        public override T_Order Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Order Find(System.Linq.Expressions.Expression<Func<T_Order, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Order.FirstOrDefault(where);
                if (item != null)
                {
                    this.AreaCode = item.AreaCode;
                    this.AssemblingInfo = item.AssemblingInfo;
                    this.Id = item.Id;
                    this.OrderCode = item.OrderCode;
                    this.OrderDate = item.OrderDate;
                    this.Phone = item.Phone;
                    this.Remark = item.Remark;
                    this.Status = item.Status;
                    this.TotalPrice = item.TotalPrice;
                    this.UserName = item.UserName;
                    this.DormitoryID = item.DormitoryID;
                    this.ProductDescript = item.ProductDescript;
                    this.HandleUser = item.HandleUser;
                    this.BusinessID = item.BusinessID;
                    this.Payway = item.Payway;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Order, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Order.Any(where);
            }
        }

        public override T_Order FindLast(System.Linq.Expressions.Expression<Func<T_Order, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Order.OrderByDescending(c=>c.Id).FirstOrDefault(where);
                if (item != null)
                {
                    this.AreaCode = item.AreaCode;
                    this.AssemblingInfo = item.AssemblingInfo;
                    this.Id = item.Id;
                    this.OrderCode = item.OrderCode;
                    this.OrderDate = item.OrderDate;
                    this.Phone = item.Phone;
                    this.Remark = item.Remark;
                    this.Status = item.Status;
                    this.TotalPrice = item.TotalPrice;
                    this.UserName = item.UserName;
                    this.DormitoryID = item.DormitoryID;
                    this.ProductDescript = item.ProductDescript;
                    this.Payway = item.Payway;
                    this.HandleUser = item.HandleUser;
                    this.BusinessID = item.BusinessID;
                    return this;
                }
                return null;
            }
        }
    }
}
